package com.lanou.controller;

import com.lanou.bean.RoleGuanLiBean;
import com.lanou.bean.RoleInfo;
import com.lanou.bean.RoleModule;
import com.lanou.service.RoleInfoService;
import com.lanou.service.RoleModuleService;
import com.lanou.utils.AjaxResult;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 18/3/10.
 */
@Controller
public class RoleController {
    @Resource
    private RoleInfoService roleInfoService;
    @Resource
    private RoleModuleService roleModuleService;

    @RequestMapping(value = "/role_add")
    public String role_add(){
        return "role/role_add";
    }


    @RequestMapping(value = "/role_modi")
    public String role_modi(){
        return "role/role_modi";
    }

    @RequestMapping(value = "/role_list")
    public String role_list(){

        if (SecurityUtils.getSubject().isPermitted("角色管理")){
            return "role/role_list";
        }else {
            return "nopower";
        }
    }

    /**
     * 添加角色信息
     * @param name
     * @param modules
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addRole")
    public AjaxResult addRole(@RequestParam("roleName")String name,@RequestParam("modules[]") List<Integer> modules){
        RoleInfo roleInfo = new RoleInfo();
        roleInfo.setName(name);
        roleInfoService.addRole(roleInfo);
        int roleId=roleInfo.getRoleId();
        RoleModule roleModule = null;
        for (Integer m : modules){
            roleModule = new RoleModule();
            roleModule.setRoleId(roleId);
            roleModule.setModuleId(m);
            roleModuleService.addRoleModule(roleModule);
        }
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setMsg("/role_list");
        return ajaxResult;


    }

    /**
     * 根据角色id删除角色信息
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delectRole")
    public AjaxResult delectRole(@RequestParam("id") int id){
        roleInfoService.deleteRole(id);
        roleModuleService.deleteModuleByRole(id);
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setMsg("/role_list");
        return ajaxResult;
    }

    /**
     * 查询所有角色信息和对应的权限
     * @return 角色和权限类的集合
     */
    @ResponseBody
    @RequestMapping(value = "/jiaoseguanli")
    public List<RoleGuanLiBean> jiaoseGuanli () {
        return roleInfoService.selectadminInfo();
    }

    /**
     * 根据角色ID查询角色信息
     * @param roleId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selectRoleInfoById")
    public RoleGuanLiBean selectRoleInfoById (@RequestParam("roleId") int roleId) {
        return roleInfoService.selectRoleInfoById(roleId);
    }

    /**
     * 根据角色id修改角色信息
     * @param roleId
     * @param name
     * @param modules
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updaterole")
    public AjaxResult updaterole (@RequestParam("roleId") int roleId , @RequestParam("name") String name ,
                                  @RequestParam("modules[]") List<Integer> modules) {


        roleInfoService.upDataRoleInfo(roleId,name);
        roleModuleService.deleteModuleByRole(roleId);
        for (int i = 0; i < modules.size() ; i++) {
            roleModuleService.insertModule(roleId,modules.get(i));
        }

        AjaxResult result = new AjaxResult();
        result.setMsg("/role_list");
        return result;
    }

    /**
     * 查询所有的角色
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/findAllRole")
    public List<RoleInfo> selectAllRoleInfo () {
        return roleInfoService.findAllRoles();
    }

}
